var Major={
	page:1,
	init:function(){
		this.AddEvent();
		this.getOneList();
	},
	initDetails:function(){
		this.MajorAddEvent();
		this.MajorDetails();
		this.GetMajorInfo();
		this.GetMajorCollege(Major.page);
		this.SchoolRanking();
	},
	AddEvent:function(){
		//点击一级获取二级
		$('#Onlist').off('click').on('click','.list-one',function(){
			var _this=$(this);
			var status=_this.attr('status');
			if(status==1){
				_this.attr('status','0');
				Major.getTwoList(_this);
			}else{
				_this.attr('status','1');
				_this.siblings('.list-two-wrap').hide();
			}
		});
			
	},
	MajorAddEvent:function(){
		$('.makeng-under').click(function(){
			fnBase.toast('内容优化中，敬请期待');
		});
		$('.under').click(function(){
			fnBase.toast('内容优化中，敬请期待');
		});
		//开设院校加载更多
		$('#schoolLoadMore').click(function(){
			Major.GetMajorCollege(Major.page);
		});
		//与原生交互去学校
		$('#ranking').on('click','.go-school',function(){
			var _this=$(this);
			var data_id=_this.attr('data_id');
			//fnBase.toast('跳学校');
			if(fnBase.versions.ios || fnBase.versions.iPhone || fnBase.versions.iPad) {
				Go2SchoolDetail(data_id);
			} else if(fnBase.versions.android) {
				window.myObj.goSchool(data_id);
			}
			
		});
		//收藏
		$('#majorCollect').on('click','.clickcollect',function(){
			var _this=$(this);
			Major.MajorCollect(_this);
		}).on('click','.clickcollectafter',function(){ //取消收藏
			var _this=$(this);
			Major.MajorCollectCancel(_this);
		})
	},
	//一级
	getOneList:function(){
		var loadingMask = document.getElementById('cssload');
		fnBase.commonAjax(fnBase.api.MajorTypeList, {}, function(data) {
			loadingMask.parentNode.removeChild(loadingMask);
			if(data.code == 0) {
				var datal = data.data;
				var str = '';
				for(var i = 0; i < datal.length; i++) {
					str += '<li><a class="list-one" status="1"  data_id="' + datal[i].id + '" major_id="' + datal[i].majorid + '" href="javascript:void(0)">' + datal[i].name + '</a><ul class="list-two-wrap"></ul></li>';
				}
				$('#Onlist').html(str);
			} else {
				fnBase.toast('亲！网络繁忙')
			}

		});
	},
	//二级
	getTwoList:function(_this){
		var _this=_this;
		var major_id=_this.attr('major_id');
		var setData={"majorid":major_id};
		fnBase.loadShow();
		fnBase.commonAjax(fnBase.api.MajorForTypeList, setData, function(data) {
			if(data.code == 0) {
				fnBase.loadHide();
				var datal = data.data;
				if(datal.length) {
					var str = '';
					for(var i = 0; i < datal.length; i++) {
						str += '<li><a class="list-two" status="1" data_id="' + datal[i].id + '" major_id="' + datal[i].majorid + '" href="javascript:void(0)">' + datal[i].majorname + '</a><ul class="list-three-wrap"></ul></li>';
					}
					_this.siblings('.list-two-wrap').html(str).show();
				} else {
					fnBase.toast('亲！暂无数据');
				}

			} else {
				fnBase.loadHide();
				fnBase.toast(data.msg);
			}
		});
		//点二级开三级
		$('#Onlist .list-two-wrap').off('click').on('click','.list-two',function(){
			var _this=$(this);
			var status=_this.attr('status');
			if(status==1){
				Major.getThreeList(_this);
				_this.attr('status','0');
			}else{
				_this.attr('status','1');
				_this.siblings('.list-three-wrap').hide();
			}
		});
	},
	//三级
	getThreeList:function(_this){
		var _this=_this;
		var major_id=_this.attr('major_id');
		var setData={"majorid":major_id};
		fnBase.loadShow();
		fnBase.commonAjax(fnBase.api.MajorForTypeList, setData, function(data) {
			if(data.code == 0) {
				fnBase.loadHide();
				var datal = data.data;
				if(datal.length) {
					var str = '';
					for(var i = 0; i < datal.length; i++) {
						str += '<li><a class="list-three" data_id="' + datal[i].id + '" major_id="' + datal[i].majorid + '" href="javascript:void(0)">' + datal[i].majorname + '</a></li>';
					}
					_this.siblings('.list-three-wrap').html(str).show();
				} else {
					fnBase.toast('亲！暂无数据');
				}
			} else {
				fnBase.loadHide();
				fnBase.toast(data.msg);
			}
		});
		//跳转
		$('#Onlist .list-two-wrap .list-three-wrap').off('click').on('click','.list-three',function(){
			var _this=$(this);
			var major_id=_this.attr('major_id');
			//console.log(major_id);
			//只需要获取major_id 传给原生
			if(fnBase.versions.ios || fnBase.versions.iPhone || fnBase.versions.iPad) {
				Go2MajorDetail(major_id);
			} else if(fnBase.versions.android) {
				window.myObj.toDetails(major_id);
			}
			//fnBase.toast('专业详情');
		});
	},
	MajorDetails:function(){
		var myContBox=$('#major_cont_wrap ul');
		$('#major_nav .major_nav_child').click(function() {
			var _this=$(this);
			goThisTop(_this);
			_this.siblings().removeClass('active');
			_this.addClass('active');
			
		});

		$('#makeng .makeng_child').click(function() {
			var _this=$(this);
			goThisTop(_this);
			_this.siblings().removeClass('ma-active');
			_this.addClass('ma-active');
		});
			function goThisTop(_this){
			var _this=_this;
			var $index = _this.index();
			var $top = myContBox.eq($index).offset().top-_this.height();
			window.scrollTo(0, $top);
		}
			//var myObjNav = document.getElementById("major_nav_box");
			var makeng = document.getElementById('makeng');
			//var OffsetTop = getTop(myObjNav);
			window.onscroll = function() {
				var bodyScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
				var myMakeng=$(makeng);
				if(bodyScrollTop > 500) {
					myMakeng.addClass('bodyUp');
				} else {
					myMakeng.removeClass('bodyUp');
				}
			};
//			function getTop(e) {
//				var offset = e.offsetTop;
//				if(e.offsetParent != null) offset += getTop(e.offsetParent);
//				return offset;
//			};		
	},
	//获取专业详情
	GetMajorInfo:function(){
		var loadingMask = document.getElementById('cssload');
		$.ajax({
			type:"post",
			url:fnBase.api.MajorInfo,
			dataType:"json",
			data:{
				"majorid":fnBase.splitURL()['major_id'],
				"user_id":fnBase.splitURL()['user_id'],
			},
			success:function(data){
				//console.log(data);
				if(data.code==0){
					loadingMask.parentNode.removeChild(loadingMask);
					var datal=data.data;
					var str='',About='',star='',IntStr='';
					var isCollect=datal.is_collect==1?'<li class="clickcollectafter">已收藏</li>':'<li class="clickcollect">收藏</li>';
					str='<li>'+datal.majorname+'</li>'+isCollect;
					star='<li>专业代码：'+datal.majorid+'</li><li>授予学位：'+datal.degree+'</li><li>学制：'+datal.study_year+'</li>';
					IntStr='<li class="content-title">教学实践</li>'+
						'<li class="major-content">'+datal.practice+'</li>'+
						'<li class="content-title">培养目标</li>'+
						'<li class="major-content">'+datal.train_aim+'</li>'+
						'<li class="content-title">培养要求</li>'+
						'<li class="major-content">'+datal.requirement+'</li>'+
						'<li class="content-title">学生能力</li>'+
						'<li class="major-content">'+datal.grad_capacity+'</li>';
						//console.log(datal.grad_capacity)
					$('#majorIntroduce').append(IntStr);
					$('.major-top-dt').html(str);
					$('.major-top-dd').html(star);
					$('#majorCourse').html(datal.course);
					for(var i=0;i<datal.about.length;i++){
						About+=datal.about[i].majorname+'、';
					}
					$('#majorAbout').html('相近专业：'+About);
				}else{
					loadingMask.parentNode.removeChild(loadingMask);
					//loadingMask.innerHTML="<p>暂无数据</p>";
					fnBase.toast('暂无数据','50%');
				}
			},
			error:function(){
                /*数据请求失败*/
               	loadingMask.parentNode.removeChild(loadingMask);
				fnBase.toast('暂无数据','50%');
            }
		});
	},
	//专业开设院校
	GetMajorCollege:function(page){
		//console.log(page);
		var page=page;
		var setData={"majorid":fnBase.splitURL()['major_id'],"user_id":fnBase.splitURL()['user_id'],"page":page};
		var str='';
		fnBase.commonAjax(fnBase.api.MajorCollege,setData,function(data){
			var datal=data.data;
			if(data.code==0){
				if(datal.length!=0){
					Major.page++;
					var str='',Batch='',Wl='';
					for(var i=0;i<datal.length;i++){
						if(datal[i].batch=='11'){Batch='一本';}else if(datal[i].batch=='12'){Batch='二本';}else{Batch='三本';};
						if(datal[i].wl=='1'){Wl='文科';}else if(datal[i].wl=='2'){Wl='理科';}else if(datal[i].wl=='3'){Wl='综合';}else{Wl='其他';};
						str+='<li data_id="'+datal[i].id+'" class="go-school"><span>'+datal[i].name+'</span><span>'+datal[i].provname+'</span><span>'+Batch+'</span><span>'+Wl+'</span><span>'+datal[i].score_avg+'</span></li>';
					};
					$('.major-content-school').append(str);
					if(datal.length<10&&page=='1'){
						$('#schoolLoadMore').hide();
					}
				}else{
					$('#schoolLoadMore').hide();
				}
			}
		});
		//与原生交互去 学校
		$('#goSchoolBox').off('click').on('click','.go-school',function(){
			var data_id=$(this).attr('data_id');
			//fnBase.toast('跳学校');
			if(fnBase.versions.ios || fnBase.versions.iPhone || fnBase.versions.iPad) {
				Go2SchoolDetail(data_id);
			} else if(fnBase.versions.android) {
				window.myObj.goSchool(data_id);
			}
			
		});
	},
	//学校排名
	SchoolRanking:function(){
//		fnBase.commonAjax(fnBase.api.MajorCollegeRank,{"majorid":fnBase.splitURL()['major_id']},function(data){
//			if(data.code==0){
//				var datal=data.data;
//				var str='',star='';
//				var dataLen=datal.length;
//				console.log(dataLen);
//				if(dataLen<=3){
//					for(var i=0;i<dataLen;i++){
//						str+='<li data_id="'+datal[i].id+'" class="go-school"><i></i><em>'+datal[i].name+'</em></li>'
//					}
//				}else{
//					for(var k=3;k<dataLen;k++){
//						var j=k+1;
//						star='<li data_id="'+datal[0].id+'" class="go-school"><i></i><em>'+datal[0].name+'</em></li>'+
//						'<li data_id="'+datal[1].id+'" class="go-school"><i></i><em>'+datal[1].name+'</em></li>'+
//						'<li data_id="'+datal[2].id+'" class="go-school"><i></i><em>'+datal[2].name+'</em></li>';
//						str+='<li data_id="'+datal[k].id+'" class="go-school"><i>'+j+'</i><em>'+datal[k].name+'</em></li>'
//					}
//				}
//				$('#ranking').append(star+str);
//			}
//		});
	},
	//收藏
	MajorCollect:function(_this){
		var _this=_this;
		var setData={"user_id":fnBase.splitURL()['user_id'],"majorid":fnBase.splitURL()['major_id']}
		fnBase.commonAjax(fnBase.api.MajorCollect,setData,function(data){
			if(data.code==0){
				_this.removeClass('clickcollect').addClass('clickcollectafter');
				_this.html('已收藏');
				fnBase.toast('收藏成功');
			}
		});
	},
	//取消收藏
	MajorCollectCancel:function(_this){
		var _this=_this;
		var setData={"user_id":fnBase.splitURL()['user_id'],"majorid":fnBase.splitURL()['major_id']}
		fnBase.commonAjax(fnBase.api.MajorCollectCancel,setData,function(data){
			if(data.code==0){
				_this.removeClass('clickcollectafter').addClass('clickcollect');
				_this.html('收藏');
				fnBase.toast('取消收藏');
			}
		});
	},
}
$(document).ready(function(){
	if($('#major_box').length>0){
		Major.initDetails();
	}else{
		Major.init();
	}
})
